import { createPinia, defineStore, storeToRefs, mapState } from 'pinia';
import { UtilTools } from 'src/plugins/utils';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
import config from 'src/config';

const { path, isCrypto } = config;
export const storage = {
	getItem: key =>  uni.getStorageSync(key) ? uni.getStorageSync(key) : {},
	setItem: (key, value) => uni.setStorageSync(key, value),
	removeItem: key => uni.removeStorageSync(key),
	clear: () => uni.clearStorageSync()
};
export const getCache = (key) => {
	const v = storage.getItem(key);
	return v ? (UtilTools.typeIs(v) === 'string' ? JSON.parse(v) : v) : null
};
export const setCache = (key, value) => storage.setItem(key, JSON.stringify(value));

export const pinia = createPinia();
export const keys = {
	'org': `${path}_org`,
	'sys': `${path}_sys`,
	'user': `${path}_user`
};

export {
	defineStore,
	piniaPluginPersistedstate,
	storeToRefs,
	mapState
};